import {
  getListPage,
  createRow,
  updateRow,
  deleteRow,
  deleteBatch
} from '@/api/projectType'
import {
  UploaderImage,
  Kindeditor
} from '@/layout/components'
export default {
  components: {
    UploaderImage,
    Kindeditor
  },
  filters: { // 字段转换
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      empty_text: '暂无数据',
      listParam: {
        pageNum: 1,
        pageSize: 5
      },
      datas: [
        // {
        //   id: 1,
        //   order: 21,
        //   name: '智慧管理',
        //   updateTime: 1571726217000
        // }
      ],
      listPages: {
        page: 1,
        size: 5,
        sizes: [5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75],
        total: 0
      },
      multipleSelection: [],
      listLoading: false,
      formDialog: {
        visible: false,
        action: '',
        title: '提示',
        width: '42rem',
        center: true
      },
      formData: {
        id: null,
        order: '',
        name: ''
      },
      formLabelWidth: '120px',
      rule: {
        name: [
          { required: true, message: '请输入项目名称', trigger: 'blur' },
          { min: 2, max: 50, message: '长度在 2 到 50 个字符', trigger: 'blur' }
        ],
        order: [{
          required: true, message: '请输入访问地址', trigger: 'blur'
        }]
      }
    }
  },
  created() {
    this._fetchData()
  },
  watch: {
    listPages: {
      handler() {
        this.listParam.pageNum = this.listPages.page
        this.listParam.pageSize = this.listPages.size
      },
      deep: true
    },
    listParam: {
      handler() {
        this._fetchData()
      },
      deep: true // 深度监听，同时也可监听到param参数变化
    }
  },
  methods: {
    _fetchData() {
      this.listLoading = true
      getListPage(this.listParam).then(response => {
        console.log(response)
        this.datas = response.result.list
        this.listPages.total = response.result.total
        // this.listLoading = false
        this.listLoading = false
      })
    },
    selectRow(selection, row) {
      this.multipleSelection = selection
      // console.log(this.multipleSelection)
    },
    selectAll(selection) {
      this.multipleSelection = selection
      // console.log(this.multipleSelection)
    },
    selectionChange(selection) {
      this.multipleSelection = selection
      // console.log(this.multipleSelection)
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.listPages.size = val
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.listPages.page = val
    },
    _fromUploaderImage(data) {
      console.log(data)
      this.formData.coverPath = data
    },
    _fromKindeditor(data) {
      // console.log(data)
      this.formData.content = data
    },
    _formDialogConfirm() {
      // console.log(this.formDialog)
      // console.log(this.formData)
      // console.info(this.formData)
      // return false
      this.$refs['form'].validate(valid => {
        if (valid) {
          switch (this.formDialog.action) {
            case 'add':
              createRow(this.formData).then(response => {
                if (response.code === 200 || response.code === 20000) {
                  this.$message({
                    type: 'success',
                    message: '操作成功!'
                  })
                  this.formDialog.visible = false
                  this._fetchData()
                } else {
                  this.$message({
                    type: 'info',
                    message: '操作失败'
                  })
                }
              })
              break
            case 'edit':
              updateRow(this.formData).then(response => {
                if (response.code === 200 || response.code === 20000) {
                  this.$message({
                    type: 'success',
                    message: '操作成功!'
                  })
                  this.formDialog.visible = false
                  this._fetchData()
                } else {
                  this.$message({
                    type: 'info',
                    message: '操作失败'
                  })
                }
              })
              break
            default:
              break
          }
        }
      })
    },
    createRow(row) {
      this.formDialog.visible = true
      this.formDialog.title = '新增'
      this.formDialog.action = 'add'
      this.formData = {
        id: null,
        order: '',
        name: ''
      }
    },
    updateRow(row) {
      this.formDialog.visible = true
      this.formDialog.title = '编辑'
      this.formDialog.action = 'edit'
      // console.log(this.formData)
      // console.log(row)
      // this.formData = row
      this.formData = {
        id: row.id,
        order: row.order,
        name: row.name
      }
    },
    detailRow(row) {
      this.formDialog.visible = true
      this.formDialog.title = '查看'
      this.formDialog.action = 'detail'
      // this.formData = row
    },
    deleteRow(row, index) {
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then((result) => {
        deleteRow({
          id: row.id
        }).then(response => {
          if (response.code === 200 || response.code === 20000) {
            this.$message({
              type: 'success',
              message: '操作成功!'
            })
            this._fetchData()
          } else {
            this.$message({
              type: 'info',
              message: '操作失败'
            })
          }
        })
      }).catch(() => {
        // this.$message({
        //   type: 'info',
        //   message: '删除取消'
        // })
      })
    },
    deleteBatch() {
      if (this.multipleSelection.length > 0) {
        this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then((result) => {
          const ids = []
          for (let i = 0; i < this.multipleSelection.length; i++) {
            ids.push(this.multipleSelection[i].id)
          }
          // console.log(ids)
          deleteBatch({
            ids: ids.join()
          }).then(response => {
            if (response.code === 200 || response.code === 20000) {
              this.$message({
                type: 'success',
                message: '操作成功!'
              })
              this._fetchData()
            } else {
              this.$message({
                type: 'error',
                message: '操作失败'
              })
            }
          })
        }).catch(() => {
          // this.$message({
          //   type: 'info',
          //   message: '删除取消'
          // })
        })
      } else {
        this.$message({
          type: 'warning',
          message: '请先选择项目类型'
        })
      }
    }
  }
}
